﻿function cusEdit() {
    var pobj = {
        data: [],
        editObj: {}
    }

    function init(obj) {
        for (var p in obj) {
            pobj[p] = obj[p];
        }
    }

    function initDatetime() {
        $('.datetimepicker').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1
        });
    }

    function initSelect() {
        $('select').each(function (idx, ele) {
            var field = $(ele).attr('name');
            $(ele).select2({
                allowClear: true,
                placeholder: "Select a state",
                data: pobj.data[field]
            });
        });
    }

    function initValue() {
        if (pobj.editObj == null) {
            pobj.editObj = {};
            $('select').val(null).trigger('change');
            return;
        }
        //console.log(JSON.stringify(pobj.editObj));
        $('*[data-type]').each(function (idx, ele) {
            var name = $(ele).attr('name');
            switch ($(ele).attr('data-type')) {
                case 'select':
                    console.log(pobj.editObj[name]);
                    if (pobj.editObj[name] != null) {
                        if ($.isArray(pobj.editObj[name].split(','))) {
                            $(ele).attr('data-val', pobj.editObj[name].split(','));
                            $(ele).val(pobj.editObj[name].split(',')).trigger('change');
                        } else {
                            $(ele).val(pobj.editObj[name]).trigger('change');
                        }
                    } else {                        
                        $(ele).val(null).trigger('change');
                    }
                    break;
                default:
                    $(ele).val(pobj.editObj[name])
                    break;
            }
        });
    }

    function refVals() {
        $('*[data-type]').each(function (idx, ele) {
            var name = $(ele).attr('name');
            if (!$.isArray($(ele).val())) {
                pobj.editObj[name] = $(ele).val();
            } else {
                var val = '';
                //$($(ele).val()).each(function (idx1, ele1) {
                //    val = val == '' ? ele1 : val + ',' + ele1;
                //});
                pobj.editObj[name] = $(ele).attr('data-val');
            }
        });
        var obj = $.extend(true, {}, pobj.editObj);
        delete obj.order;
        return obj;
    }

    var editObj = {
        init: init,
        initEdit: function () {
            initDatetime();
            initSelect();
            initValue();
        },
        refObj: function () {
            return refVals();
        },
        obj: pobj
    }

    return editObj;
}

var get_accordionIdx = function (obj) {
    var id = $(obj).parents('div.panel-collapse.collapse:eq(0)').attr('id');
    id = parseInt(id.replace('collapse', ''));
    return id;
}

var cus_accordion = function (obj) {
    var id = $(obj).parents('div.panel-collapse.collapse:eq(0)').attr('id');
    id = parseInt(id.replace('collapse', '')) + 1;
    id = '#collapse' + id;
    $('.panel-collapse.collapse').removeClass('in');
    $(id).collapse('toggle')
}